<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||	
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws. 
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.). 
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations). 
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Smiles/classes/PhpFox_ServiceSmiles.class.php, $Author:  $ */



/** Service for working smilies functionality
 * 
 * @package module.smile
 * @version $Id: 
 *   PHPFox Team ()
 */
class PhpFox_Mod_Smiles_ServiceSmiles extends BaseService
{
    
    function _checkFile($sFile)
    {
        $oSmiles = &$this->_oModule->getItem('Smiles');
        if(!CFile::isWritable($sFile))
        {
            $this->_addError('Mod_Smiles.item.notallowed');
            return false;
        }
        
        
        return true;
    }
    
    function delItems($sFile)
    {
        $oSmiles = $this->_oModule->getItem('Smiles');
        
        $sPatch = App::getParam('smile.path').'emo/';
        $sFileName = base64_decode($sFile);
        if(!file_exists($sPatch.$sFileName))
        {
            $this->_addError('Mod_Smiles.item.notexists');
            return false;
        }
        if($this->_checkFile($sPatch.$sFileName))
            if(!unlink($sPatch.$sFileName))
                $this->_addError('Mod_Smiles.item.notdeleted');
                
        return true;
    }
    
    function renameSmile($sFile, $sNewName, $sExt)
    {
        $oSmiles = $this->_oModule->getItem('Smiles');
        $sPatch = App::getParam('smile.path').'emo/';
        
        if(($sFile != $sNewName.'.'.$sExt) && file_exists($sPatch.$sNewName.'.'.$sExt))
        {
            $this->_addError('Mod_Smiles.item.exists');
            return false;
        }
        if($this->_checkFile($sPatch.$sFile))
            if(!rename($sPatch.$sFile, $sPatch.$sNewName.'.'.$sExt))
                $this->_addError('Mod_Smiles.item.notrename');
        @chmod($sPatch.$sNewName.'.'.$sExt, 0755);                 
        return true;    
    }
    
    function uploadSmiles($aFiles)
    {
        $oSmiles = $this->_oModule->getItem('Smiles');
        $oImage = &App::newObject('file_Image');
        $aArg = App::getParam('smilie');
        $oImage->aLimits['MAX_IMAGE_W'] = $aArg['max_w'];
        $oImage->aLimits['MAX_IMAGE_H'] = $aArg['max_h'];
        $oImage->aLimits['MIN_IMAGE_W'] = $aArg['min_w'];
        $oImage->aLimits['MIN_IMAGE_H'] = $aArg['min_h'];
        $oImage->aLimits['FILE_MAX_SIZE'] = $aArg['max_s'];
        $oImage->aLimits['PERMITTED_TYPES'] = $aArg['type'];
        $sSmilePath = App::getParam('smile.path').'emo/';
        $iEmpty = 0;
        foreach ($aFiles['image']['error'] as $i=>$mValue)
        {
            if(!$mValue)
            {
                list($sFileName, $sExt)  =  explode('.', $aFiles['image']['name'][$i]);
                if(strlen($sFileName) > 30)
                {
                    $this->addErrors(App::format('Mod_Smilies.item.long'));
                    continue;
                }
                if(file_exists($sSmilePath.$aFiles['image']['name'][$i]))
                {
                    $this->addErrors(App::format('Mod_Smiles.item.exists'));
                    continue;
                }
                if (!$oImage->upload('image['.$i.']', $sSmilePath, $sFileName, 'gif', 0755))
                    $this->addErrors($oImage->getErrors());
            }
            else
                $iEmpty++;
        }
        if($iEmpty == count($aFiles['image']['error']))
            $this->addErrors(App::format('Mod_Smilies.item.empty'));
        
        $aErrors = $this->getErrors();

        return !empty($aErrors) ? false : true;
    }
}
?>